/**
 * Created by 白雨浓 on 2019/1/16 15:50.
 */
const ast = {
  items: [],
};

const handleAst = (lines) => {
  ast.items = [];

  lines.forEach(line => {
    line = line.trim();
    const item = {};

    item.field = line.split(':')[0];

    const reg = /\/\*.*\*\//;
    if (reg.test(line)) {

      const str = line.split(',')[1]
        .replace('/*', '')
        .replace('*/', '')
        .trim();

      const annotations = str.split(' ');

      item.annotation = annotations[0];
      item.type = annotations[1];
    }

    ast.items.push(item);
  });

};

export default (entity) => {
  let lines = entity.trim().split('\n');
  if (lines[0].indexOf('{') >= 0) {
    lines = lines.splice(1, lines.length - 2);
  }

  handleAst(lines);

  return ast;
}
